package com.alasbusy.alasbusyapp.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.base.BaseActivity;

public class WelcomeActivity extends BaseActivity {

    private ImageView cao;
    private ImageView ai;
    private ImageView tanqi;
    private ImageView haomang;
    private ImageView bolang;
    private ImageView zi_gai;

    @Override
    protected void initView() {
        cao = findViewById(R.id.cao);
        ai = findViewById(R.id.ai);
        tanqi = findViewById(R.id.tanqi);
        haomang = findViewById(R.id.haomang);
        bolang = findViewById(R.id.bolang);
        zi_gai = findViewById(R.id.zi_gai);
        cao.setVisibility(View.INVISIBLE);
        ai.setVisibility(View.INVISIBLE);
        haomang.setVisibility(View.INVISIBLE);
        bolang.setVisibility(View.INVISIBLE);
        zi_gai.setVisibility(View.INVISIBLE);
        //AlpahAnimation(ai,tanqi,cao,haomang,bolang,zi_gai);

    }

    @Override
    protected void onStart() {
        AiAnim();
        //AlpahAnimation(ai,tanqi,cao,haomang,bolang,zi_gai);
        super.onStart();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public int getLayout() {
        return R.layout.activity_welcome;
    }

    private void AiAnim(){
        cao.setVisibility(View.VISIBLE);
        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(cao, "translationY", -100, 0);  //cao
        AnimatorSet set = new AnimatorSet();
        set.play(transYAnim);
        set.setDuration(2000);
        set.start();
        transYAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                ai.setVisibility(View.VISIBLE);
                ObjectAnimator aiAnim = ObjectAnimator.ofFloat(ai, "alpha", 0.0f, 0.5f, 0.8f, 1.0f);  //ai
                AnimatorSet aiset = new AnimatorSet();
                aiset.play(aiAnim);
                aiset.setDuration(2000);
                aiset.start();

                aiAnim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        tanqi.setVisibility(View.VISIBLE);
                        ObjectAnimator tanqiAnim = ObjectAnimator.ofFloat(tanqi, "alpha", 0.0f, 0.5f, 0.8f, 1.0f,0.8f,0.0f); //tanqi
                        AnimatorSet tanqiset = new AnimatorSet();
                        tanqiset.play(tanqiAnim);
                        tanqiset.setDuration(2000);
                        tanqiset.start();

                        tanqiAnim.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                haomang.setVisibility(View.VISIBLE);
                                ObjectAnimator haomangAnim = ObjectAnimator.ofFloat(haomang, "alpha", 0.0f, 0.5f, 0.8f, 1.0f); //haomang
                                AnimatorSet haomangset = new AnimatorSet();
                                haomangset.play(haomangAnim);
                                haomangset.setDuration(2000);
                                haomangset.start();

                                haomangAnim.addListener(new AnimatorListenerAdapter() {
                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        zi_gai.setVisibility(View.VISIBLE);
                                        ObjectAnimator zigaiAnim = ObjectAnimator.ofFloat(zi_gai, "rotation", 0, 360);  //zigai
                                        AnimatorSet zigaiset = new AnimatorSet();
                                        zigaiset.play(zigaiAnim);
                                        zigaiset.setDuration(1000);
                                        zigaiset.start();
                                        zigaiAnim.addListener(new AnimatorListenerAdapter() {
                                            @Override
                                            public void onAnimationEnd(Animator animation) {
                                                bolang.setVisibility(View.VISIBLE);
                                                ObjectAnimator bolangAnim = ObjectAnimator.ofFloat(bolang, "translationX", 400, 0);  //bolang
                                                AnimatorSet bolangset = new AnimatorSet();
                                                bolangset.play(bolangAnim);
                                                bolangset.setDuration(2000);
                                                bolangset.start();

                                                bolangAnim.addListener(new AnimatorListenerAdapter() {
                                                    @Override
                                                    public void onAnimationEnd(Animator animation) {
                                                        new Handler().postDelayed(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
                                                                WelcomeActivity.this.finish();
                                                            }
                                                        },800);
                                                    }
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });

        /*final ObjectAnimator tanqiAnim = ObjectAnimator.ofFloat(tanqi, "alpha", 1.0f, 0.5f, 0.8f, 1.0f,0.8f,0.0f);

        aiAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                set.play(tanqiAnim);
                set.setDuration(4000);
                set.start();
            }
        });*/
    }

    //淡入淡出效果
    private void AlpahAnimation(ImageView imgview1,ImageView imgview2,ImageView imgview3,ImageView imgview4,ImageView imgview5,ImageView imgview6) {
        ObjectAnimator aiAnim = ObjectAnimator.ofFloat(imgview1, "alpha", 0.0f, 0.5f, 0.8f, 1.0f);  //ai

        ObjectAnimator tanqiAnim = ObjectAnimator.ofFloat(imgview2, "alpha", 0.0f, 0.5f, 0.8f, 1.0f,0.8f,0.0f); //tanqi

        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(imgview3, "translationY", -100, 0);  //cao

        ObjectAnimator haomangAnim = ObjectAnimator.ofFloat(imgview4, "alpha", 0.0f, 0.5f, 0.8f, 1.0f); //haomang

        ObjectAnimator bolangAnim = ObjectAnimator.ofFloat(imgview5, "translationX", 400, 0);  //bolang

//        ObjectAnimator scaleXAnim = ObjectAnimator.ofFloat(view, "scaleX", 0.0f, 1.0f);
//        ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(view, "scaleY", 0.0f, 2.0f);
        ObjectAnimator zigaiAnim = ObjectAnimator.ofFloat(imgview6, "rotation", 0, 360);  //zigai


        //ObjectAnimator transYAnim = ObjectAnimator.ofFloat(imgview, "tranlsationY", 100, 750);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(aiAnim, tanqiAnim, transYAnim, haomangAnim, bolangAnim, zigaiAnim);
       // set.playSequentially(aiAnim, tanqiAnim, transYAnim, haomangAnim, bolangAnim, zigaiAnim);
        //                set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
        set.setDuration(6000);
        set.start();

        tanqiAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                tanqi.setVisibility(View.INVISIBLE);
            }
        });

        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {

            }
        });
    }

}
